//http结果解析:true 正常 false 异常
const httpIsWrong = (res) => {
	if (res.code != 0) {
		uni.showToast({
			icon: 'error',
			title: '网络异常',
			duration: 1500
		})
		return true;
	} else {
		if (res.json.data.code != 200) {
			uni.showToast({
				icon: 'none',
				title: res.json.data.msg,
				duration: 1500
			})
			return true;
		} else {
			return false;
		}
	}
}

//登录页面
const pageIndex = () => {
	uni.redirectTo({
		url: "/pages/index/index"
	})
}

//加密
const encode = (str) => {
	var bytes = [];
	for (var i = 0; i < str.length; i++) {
		bytes.push(str.charCodeAt(i));
	}
	return bytes.join('l');
}

//解密 
const decode = (str) => {
	return String.fromCharCode.apply(null, str.trim().split("l"));
}

//判断点是否在面内
const isInPolygon = (checkPoint, polygonPoints) => {
	var counter = 0;
	var i;
	var xinters;
	var p1, p2;
	var pointCount = polygonPoints.length;
	p1 = polygonPoints[0];

	for (i = 1; i <= pointCount; i++) {
		p2 = polygonPoints[i % pointCount];
		if (
			checkPoint[0] > Math.min(p1[0], p2[0]) &&
			checkPoint[0] <= Math.max(p1[0], p2[0])
		) {
			if (checkPoint[1] <= Math.max(p1[1], p2[1])) {
				if (p1[0] != p2[0]) {
					xinters =
						(checkPoint[0] - p1[0]) *
						(p2[1] - p1[1]) /
						(p2[0] - p1[0]) +
						p1[1];
					if (p1[1] == p2[1] || checkPoint[1] <= xinters) {
						counter++;
					}
				}
			}
		}
		p1 = p2;
	}
	if (counter % 2 == 0) {
		return false;
	} else {
		return true;
	}
}

module.exports = {
	pageIndex,
	httpIsWrong,
	encode,
	decode,
	isInPolygon
}